package com.info.zhiduoduo.pay.sdk.dayspaysdk.util;

import com.info.zhiduoduo.pay.sdk.dayspaysdk.core.crpyter.Rsa;
import com.info.zhiduoduo.pay.sdk.dayspaysdk.core.crpyter.TripleDes;
import com.info.zhiduoduo.pay.sdk.dayspaysdk.core.util.Base64;
import com.info.zhiduoduo.pay.sdk.dayspaysdk.core.util.Reflecter;
import com.info.zhiduoduo.pay.sdk.dayspaysdk.core.util.Strings;

import java.util.HashMap;
import java.util.LinkedHashMap;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SecurityUtil {

	public static LinkedHashMap<String,String> parseResponse(String response, String[] signFields, String myPfxPath, String myPfxPwd, String easyPublicKey) throws Exception {

		String des3KeyRsaEncryptedString = response.split("\\|")[0];
		String des3EncryptString = response.split("\\|")[1];
		String des3Key = Rsa.decrypt(des3KeyRsaEncryptedString, myPfxPath, myPfxPwd);
		String json = TripleDes.decrypt(des3Key, des3EncryptString);

		LinkedHashMap<String,String> bean = Reflecter.jsonToMapStr(json);
		if("0000".equals(bean.get("responseCode"))) {
			if(!verify(bean,signFields, Strings.toString(bean.get("signMsg")),easyPublicKey)) {
				bean.put("responseCode", "E004");
				bean.put("responseRemark", "验签失败");
			}
		}

		return bean;
	}

	public static String encrypt(String json,String easyPublicKey) throws Exception {

		String des3Key = Base64.encode(Strings.randomHex(24).getBytes());
		String des3EncryptString = TripleDes.encrypt(des3Key, json);
		String des3KeyRsaEncryptedString = Rsa.encrypt(des3Key, easyPublicKey);

		return des3KeyRsaEncryptedString+"|"+des3EncryptString;
	}

	private static String toSignString(HashMap<String,String> bean,String[] signFields) {

		StringBuffer signString = new StringBuffer();
		for(String field:signFields) {
			String tmp = Strings.toString(bean.get(field));
			if(!Strings.isNullOrEmpty(tmp)) {
				signString.append(tmp).append(" ");
			}
		}

		String string = signString.toString();
		if(string.length() > 0) {
			string = string.substring(0,string.length()-1);
		}

		log.info("msg sign string:["+string+"]");

		return string;
	}

	public static String sign(HashMap<String,String> bean, String[] signFields, String myPfxPath, String myPfxPwd) throws Exception {

		String string = toSignString(bean,signFields);

		String ret = Rsa.sign(string, myPfxPath, myPfxPwd, algorithm);

		return ret;
	}

	private static String algorithm = "SHA1withRSA";//SHA1withRSA//SHA1withRSA
	public static boolean verify(HashMap<String,String> bean,String[] signFields,String signMsg,String publicKey) throws Exception {

		String string = toSignString(bean,signFields);

		return Rsa.verify(signMsg, publicKey, string, algorithm);
	}

	public static void main(String[] args) throws Exception {

		//String signMsg = "Lt9ms44/3TsDqtLa/C0E7OWcCKngxhOJ5pwCa5dSWygZF1VzIOlC9jNb/8kK7yMYqLdkmGVzjvaXdl/fqT0hE/GrAX8FF0cJddnLlcE2UJBSKQRs0TwDQvcrYWHwb3A6SR7bjgYukzLLkzCj3aNZ+p+4pvHYGErCTTpKWeNo7fc=";
		//String signData = "i want to xxx you:[0200 190011 111 2016110657248621 06635595 0.1 156]ppppp";
		//boolean v = Rsa.verify(signMsg, Config.merchantPublicKey, signData, "SHA256withRSA");

		//String s = Rsa.sign(signData, Config.merchantPrivateKeyPath, Config.merchantPrivateKeyPwd, "SHA256withRSA");
		//System.out.println(s);
		//System.out.println(v);

		//String enc = "BPRaO0Nn7t0n/SEYcR7YxvTbnIwCxhnRltlAByuKq6AvXImmBCI+0siFWxd5clXzPZ/jcrzDTIsUTAoUwUFErPx1JnRYxKX8iZsqoRRSRhYdTFj4uLklgDVeL660Ze+cxeWZbL+oQBuPM8uA2SKwzw+r8ALYx/J0b+1umJYRzm8=";
		//System.out.println(new String(Base64.decode(dec)));

		//enc = Rsa.encrypt(signData, Config.merchantPublicKey);
		//System.out.println(enc);

	}

}
